package jltx.fragmentpageframework.simple;

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import cn.jltx.helper.FragmentLifecycleCallbacks;
import cn.jltx.support.SupportAppCompatActivity;
import cn.jltx.support.SupportFragment;
import cn.jltx.utils.ViewHelper;
import jltx.fragmentpageframework.R;
import jltx.fragmentpageframework.simple.fragment.base.BaseMainFragment;
import jltx.fragmentpageframework.simple.fragment.home.HomeFragment;
import jltx.fragmentpageframework.simple.fragment.pager.NewsTabFragment;
import jltx.fragmentpageframework.simple.fragment.shop.ShopFragment;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2016年01月29日.
 */
public class SimpleActivity extends SupportAppCompatActivity implements BaseMainFragment.OnFragmentToggerDrawerListener {
    // 再点一次退出程序时间设置
    private static final long WAIT_TIME = 2000L;
    private long TOUCH_TIME = 0;

    public static final int HOME = 0;
    public static final int PAGE = 1;
    public static final int SHOP = 2;
    public static final String NAV_DEFAULT_INDEX = "nav_default_index";

    private SupportFragment[] mFragments = new SupportFragment[3];

    private int defaultPage = HOME;

    private DrawerLayout drawerLayout;
    private NavigationView navigationView;

    private int[] navMenuId = {R.id.nav_simple_home,R.id.nav_simple_tab,R.id.nav_simple_shop};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple);
        if(savedInstanceState == null){
            mFragments[HOME] = new HomeFragment();
            mFragments[PAGE] = new NewsTabFragment();
            mFragments[SHOP] = new ShopFragment();
            loadMultipleRootFragment(R.id.fragment_cotainer,defaultPage,mFragments);
        }else {
            defaultPage = savedInstanceState.getInt(NAV_DEFAULT_INDEX);
            mFragments[HOME] = findFragment(HomeFragment.class);
            mFragments[PAGE] = findFragment(NewsTabFragment.class);
            mFragments[SHOP] = findFragment(ShopFragment.class);
        }
        registerFragmentLifecycleCallbacks(new FragmentLifecycleCallbacks() {
            // 可以监听该Activity下的所有Fragment的18个 生命周期方法

            @Override
            public void onFragmentCreated(SupportFragment fragment, Bundle savedInstanceState) {
                Log.i("RRRRR", "onFragmentCreated--->" + fragment.getClass().getSimpleName());
            }

            @Override
            public void onFragmentSaveInstanceState(SupportFragment fragment, Bundle outState) {
                super.onFragmentSaveInstanceState(fragment, outState);
                Log.i("RRRRR", "onFragmentSaveInstanceState--->" + fragment.getClass().getSimpleName());
            }

            @Override
            public void onFragmentEnterAnimationEnd(SupportFragment fragment, Bundle savedInstanceState) {
                super.onFragmentEnterAnimationEnd(fragment, savedInstanceState);
                Log.i("RRRRR", "onFragmentEnterAnimationEnd--->" + fragment.getClass().getSimpleName());
            }

            @Override
            public void onFragmentLazyInitView(SupportFragment fragment, Bundle savedInstanceState) {
                super.onFragmentLazyInitView(fragment, savedInstanceState);
                Log.i("RRRRR", "onFragmentLazyInitView--->" + fragment.getClass().getSimpleName());
            }

            @Override
            public void onFragmentSupportVisible(SupportFragment fragment) {
                super.onFragmentSupportVisible(fragment);
                Log.i("RRRRR", "onFragmentSupportVisible--->" + fragment.getClass().getSimpleName());
            }

            @Override
            public void onFragmentSupportInvisible(SupportFragment fragment) {
                super.onFragmentSupportInvisible(fragment);
                Log.i("RRRRR", "onFragmentSupportInvisible--->" + fragment.getClass().getSimpleName());
            }

            @Override
            public void onFragmentAttached(SupportFragment fragment) {
                super.onFragmentAttached(fragment);
                Log.i("RRRRR", "onFragmentAttached--->" + fragment.getClass().getSimpleName());
            }

            @Override
            public void onFragmentStarted(SupportFragment fragment) {
                super.onFragmentStarted(fragment);
                Log.i("RRRRR", "onFragmentStarted--->" + fragment.getClass().getSimpleName());
            }

            @Override
            public void onFragmentResumed(SupportFragment fragment) {
                super.onFragmentResumed(fragment);
                Log.i("RRRRR", "onFragmentResumed--->" + fragment.getClass().getSimpleName());
            }

            @Override
            public void onFragmentPaused(SupportFragment fragment) {
                super.onFragmentPaused(fragment);
                Log.i("RRRRR", "onFragmentPaused--->" + fragment.getClass().getSimpleName());
            }

            @Override
            public void onFragmentStopped(SupportFragment fragment) {
                super.onFragmentStopped(fragment);
                Log.i("RRRRR", "onFragmentStopped--->" + fragment.getClass().getSimpleName());
            }

            @Override
            public void onFragmentDestroyView(SupportFragment fragment) {
                super.onFragmentDestroyView(fragment);
                Log.i("RRRRR", "onFragmentDestroyView--->" + fragment.getClass().getSimpleName());
            }

            @Override
            public void onFragmentDestroyed(SupportFragment fragment) {
                super.onFragmentDestroyed(fragment);
                Log.i("RRRRR", "onFragmentDestroyed--->" + fragment.getClass().getSimpleName());
            }

            @Override
            public void onFragmentDetached(SupportFragment fragment) {
                super.onFragmentDetached(fragment);
                Log.i("RRRRR", "onFragmentDetached--->" + fragment.getClass().getSimpleName());
            }

            @Override
            public void onFragmentActivityCreated(SupportFragment fragment, Bundle savedInstanceState) {
                super.onFragmentActivityCreated(fragment, savedInstanceState);
                Log.i("RRRRR", "onFragmentActivityCreated--->" + fragment.getClass().getSimpleName());
            }

            @Override
            public void onFragmentViewCreated(SupportFragment fragment, Bundle savedInstanceState) {
                super.onFragmentViewCreated(fragment, savedInstanceState);
                Log.i("RRRRR", "onFragmentViewCreated--->" + fragment.getClass().getSimpleName());
            }
        });
        initView();
    }

    private void initView(){
        drawerLayout = ViewHelper.findViewById(this,R.id.drawer_simple);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawerLayout,R.string.nav_simple_drawer_open,R.string.nav_simple_drawer_close);
        toggle.syncState();

        navigationView = ViewHelper.findViewById(this,R.id.nav_simple);
        navigationView.setCheckedItem(navMenuId[defaultPage]);
        navigationView.setNavigationItemSelectedListener(navItemSelectedListener);
    }
    @Override
    public void onBackPressedSupport() {
        if(drawerLayout.isDrawerOpen(navigationView)){
            drawerLayout.closeDrawers();
        }else if(isRootInTop()){
            super.onBackPressedSupport();
        }else {
            if (System.currentTimeMillis() - TOUCH_TIME < WAIT_TIME) {
                super.onBackPressedSupport();
            } else {
                TOUCH_TIME = System.currentTimeMillis();
                Toast.makeText(this, R.string.press_again_exit, Toast.LENGTH_SHORT).show();
            }
        }
    }

    private NavigationView.OnNavigationItemSelectedListener navItemSelectedListener = new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            int lastDefaultPage = defaultPage;
            switch (item.getItemId()){
                case R.id.nav_simple_home:
                    defaultPage = HOME;
                    break;
                case R.id.nav_simple_tab:
                    defaultPage = PAGE;
                    break;
                case R.id.nav_simple_shop:
                    defaultPage = SHOP;
                    break;
                default:
                    break;
            }
            if(lastDefaultPage != defaultPage){
                showHideFragment(mFragments[defaultPage],mFragments[lastDefaultPage]);
            }
            drawerLayout.closeDrawers();
            return true;
        }
    };

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putInt(NAV_DEFAULT_INDEX,defaultPage);
    }

    @Override
    public boolean taggerDrawer() {
        if(drawerLayout.isDrawerOpen(navigationView)){
            drawerLayout.closeDrawers();
            return true;
        }
        return false;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        return super.onOptionsItemSelected(item);
    }
}
